<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Engines | Auto.js Pro-8.1.0 文档</title>
  <link rel="stylesheet" href="assets/fonts.css">
  <link rel="stylesheet" href="assets/style.css">
  <link rel="stylesheet" href="assets/sh.css">
  <link rel="canonical" href="https://nodejs.org/api/engines.html">
</head>
<body class="alt apidoc" id="api-section-engines">
  <div id="content" class="clearfix">
    <div id="column2" class="interior">
      <div id="intro" class="interior">
        <a href="/" title="返回首页">
          Auto.js
        </a>
      </div>
      <ul>
<li><a class="nav-documentation" href="documentation.html">关于本文档</a></li>
<li><a class="nav-http-www-w3school-com-cn-js-pro_js_syntax-asp" href="http://www.w3school.com.cn/js/pro_js_syntax.asp">W3CSchool - ECMAScript教程</a></li>
<li><a class="nav-http-es6-ruanyifeng-com-README" href="http://es6.ruanyifeng.com/#README">阮一峰 - ECMAScript 6教程</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a class="nav-overview" href="overview.html">Overview - 综述</a></li>
<li><a class="nav-qa" href="qa.html">Q&amp;A - 常见问题</a></li>
<li><a class="nav-app" href="app.html">App - 应用</a></li>
<li><a class="nav-console" href="console.html">Console - 控制台</a></li>
<li><a class="nav-coordinates-based-automation" href="coordinates-based-automation.html">CoordinatesBasedAutomation - 基于坐标的操作</a></li>
<li><a class="nav-crypto" href="crypto.html">Crypto - 加解密与消息摘要</a></li>
<li><a class="nav-device" href="device.html">Device - 设备</a></li>
<li><a class="nav-dialogs" href="dialogs.html">Dialogs - 对话框</a></li>
<li><a class="nav-engines active" href="engines.html">Engines - 脚本引擎</a></li>
<li><a class="nav-events" href="events.html">Events - 事件与监听</a></li>
<li><a class="nav-floaty" href="floaty.html">Floaty - 悬浮窗</a></li>
<li><a class="nav-files" href="files.html">Files - 文件系统</a></li>
<li><a class="nav-globals" href="globals.html">Globals - 一般全局函数</a></li>
<li><a class="nav-http" href="http.html">Http - HTTP</a></li>
<li><a class="nav-images" href="images.html">Images - 图片与图色处理</a></li>
<li><a class="nav-keys" href="keys.html">Keys - 按键模拟</a></li>
<li><a class="nav-media" href="media.html">Media - 多媒体</a></li>
<li><a class="nav-modules" href="modules.html">Modules - 模块</a></li>
<li><a class="nav-widgets-based-automation" href="widgets-based-automation.html">WidgetsBasedAutomation - 基于控件的操作</a></li>
<li><a class="nav-sensors" href="sensors.html">Sensors - 传感器</a></li>
<li><a class="nav-shell" href="shell.html">Shell - Shell命令</a></li>
<li><a class="nav-storages" href="storages.html">Storages - 本地存储</a></li>
<li><a class="nav-threads" href="threads.html">Threads - 多线程</a></li>
<li><a class="nav-timers" href="timers.html">Timers - 定时器</a></li>
<li><a class="nav-ui" href="ui.html">UI - 用户界面</a></li>
<li><a class="nav-https-developer-mozilla-org-zh-CN-docs-Mozilla-Projects-Rhino-Scripting_Java" href="https://developer.mozilla.org/zh-CN/docs/Mozilla/Projects/Rhino/Scripting_Java">Work with Java - 调用Java API</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a class="nav-https-github-com-hyb1996-NoRootScriptDroid" href="https://github.com/hyb1996/NoRootScriptDroid">GitHub项目 &amp; Issue提交</a></li>
<li><a class="nav-http-autojs-org" href="http://autojs.org">Auto.js交流社区</a></li>
</ul>

    </div>

    <div id="column1" data-id="engines" class="interior">
      <header>
        <h1>Auto.js Pro-8.1.0 文档</h1>
        <div id="gtoc">
          <p>
            <a href="index.html" name="toc">索引</a> |
            <a href="all.html">查看全部</a>
          </p>
        </div>
        <hr>
      </header>

      <div id="toc">
        <h2>目录</h2>
        <ul>
<li><span class="stability_2"><a href="#engines_engines">Engines</a></span><ul>
<li><span class="stability_undefined"><a href="#engines_engines_execscript_name_script_config">engines.execScript(name, script[, config])</a></span></li>
<li><span class="stability_undefined"><a href="#engines_engines_execscriptfile_path_config">engines.execScriptFile(path[, config])</a></span></li>
<li><span class="stability_undefined"><a href="#engines_engines_execautofile_path_config">engines.execAutoFile(path[, config])</a></span></li>
<li><span class="stability_undefined"><a href="#engines_engines_stopall">engines.stopAll()</a></span></li>
<li><span class="stability_undefined"><a href="#engines_engines_stopallandtoast">engines.stopAllAndToast()</a></span></li>
<li><span class="stability_undefined"><a href="#engines_engines_myengine">engines.myEngine()</a></span></li>
<li><span class="stability_undefined"><a href="#engines_engines_all">engines.all()</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#engines_scriptexecution">ScriptExecution</a></span><ul>
<li><span class="stability_undefined"><a href="#engines_scriptexecution_getengine">ScriptExecution.getEngine()</a></span></li>
<li><span class="stability_undefined"><a href="#engines_scriptexecution_getconfig">ScriptExecution.getConfig()</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#engines_scriptengine">ScriptEngine</a></span><ul>
<li><span class="stability_undefined"><a href="#engines_scriptengine_forcestop">ScriptEngine.forceStop()</a></span></li>
<li><span class="stability_undefined"><a href="#engines_scriptengine_cwd">ScriptEngine.cwd()</a></span></li>
<li><span class="stability_undefined"><a href="#engines_scriptengine_getsource">ScriptEngine.getSource()</a></span></li>
<li><span class="stability_undefined"><a href="#engines_scriptengine_emit_eventname_args">ScriptEngine.emit(eventName[, ...args])</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#engines_scriptconfig">ScriptConfig</a></span><ul>
<li><span class="stability_undefined"><a href="#engines_delay">delay</a></span></li>
<li><span class="stability_undefined"><a href="#engines_interval">interval</a></span></li>
<li><span class="stability_undefined"><a href="#engines_looptimes">loopTimes</a></span></li>
<li><span class="stability_undefined"><a href="#engines_getpath">getPath()</a></span></li>
</ul>
</li>
</ul>

      </div>

      <div id="apicontent">
        <h1>Engines<span><a class="mark" href="#engines_engines" id="engines_engines">#</a></span></h1>
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>engines模块包含了一些与脚本环境、脚本运行、脚本引擎有关的函数，包括运行其他脚本，关闭脚本等。</p>
<p>例如，获取脚本所在目录：</p>
<pre><code>toast(engines.myEngine().cwd());
</code></pre><h2>engines.execScript(name, script[, config])<span><a class="mark" href="#engines_engines_execscript_name_script_config" id="engines_engines_execscript_name_script_config">#</a></span></h2>
<div class="signature"><ul>
<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要运行的脚本名称。这个名称和文件名称无关，只是在任务管理中显示的名称。</li>
<li><code>script</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要运行的脚本内容。</li>
<li><code>config</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 运行配置项<ul>
<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 延迟执行的毫秒数，默认为0</li>
<li><code>loopTimes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行次数，默认为1。0为无限循环。</li>
<li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行时两次运行之间的时间间隔，默认为0</li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 指定脚本运行的目录。这些路径会用于require时寻找模块文件。</li>
</ul>
</li>
</ul>
</div><p>在新的脚本环境中运行脚本script。返回一个<a href="#engines_scriptexecution">ScriptExectuion</a>对象。</p>
<p>所谓新的脚本环境，指定是，脚本中的变量和原脚本的变量是不共享的，并且，脚本会在新的线程中运行。</p>
<p>最简单的例子如下：</p>
<pre><code>engines.execScript(&quot;hello world&quot;, &quot;toast(&#39;hello world&#39;)&quot;);
</code></pre><p>如果要循环运行，则：</p>
<pre><code>//每隔3秒运行一次脚本，循环10次
engines.execScript(&quot;hello world&quot;, &quot;toast(&#39;hello world&#39;)&quot;, {
    loopTimes: 10,
    interval: 3000
});
</code></pre><p>用字符串来编写脚本非常不方便，可以结合 <code>Function.toString()</code>的方法来执行特定函数:</p>
<pre><code>function helloWorld(){
    //注意，这里的变量和脚本主体的变量并不共享
    toast(&quot;hello world&quot;);
}
engines.execScript(&quot;hello world&quot;, &quot;helloWorld();\n&quot; + helloWorld.toString());
</code></pre><p>如果要传递变量，则可以把这些封装成一个函数：</p>
<pre><code>function exec(action, args){
    args = args || {};
    engines.execScript(action.name, action.name + &quot;(&quot; + JSON.stringify(args) + &quot;);\n&quot; + action.toString());
}

//要执行的函数，是一个简单的加法
function add(args){
    toast(args.a + args.b);
}

//在新的脚本环境中执行 1 + 2
exec(add, {a: 1, b:2});
</code></pre><h2>engines.execScriptFile(path[, config])<span><a class="mark" href="#engines_engines_execscriptfile_path_config" id="engines_engines_execscriptfile_path_config">#</a></span></h2>
<div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要运行的脚本路径。</li>
<li><code>config</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 运行配置项<ul>
<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 延迟执行的毫秒数，默认为0</li>
<li><code>loopTimes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行次数，默认为1。0为无限循环。</li>
<li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行时两次运行之间的时间间隔，默认为0</li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 指定脚本运行的目录。这些路径会用于require时寻找模块文件。</li>
</ul>
</li>
</ul>
</div><p>在新的脚本环境中运行脚本文件path。返回一个<a href="#ScriptExecution">ScriptExecution</a>对象。</p>
<pre><code>engines.execScriptFile(&quot;/sdcard/脚本/1.js&quot;);
</code></pre><h2>engines.execAutoFile(path[, config])<span><a class="mark" href="#engines_engines_execautofile_path_config" id="engines_engines_execautofile_path_config">#</a></span></h2>
<div class="signature"><ul>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 要运行的录制文件路径。</li>
<li><code>config</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> 运行配置项<ul>
<li><code>delay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 延迟执行的毫秒数，默认为0</li>
<li><code>loopTimes</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行次数，默认为1。0为无限循环。</li>
<li><code>interval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> 循环运行时两次运行之间的时间间隔，默认为0</li>
<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 指定脚本运行的目录。这些路径会用于require时寻找模块文件。</li>
</ul>
</li>
</ul>
</div><p>在新的脚本环境中运行录制文件path。返回一个<a href="#ScriptExecution">ScriptExecution</a>对象。</p>
<pre><code>engines.execAutoFile(&quot;/sdcard/脚本/1.auto&quot;);
</code></pre><h2>engines.stopAll()<span><a class="mark" href="#engines_engines_stopall" id="engines_engines_stopall">#</a></span></h2>
<p>停止所有正在运行的脚本。包括当前脚本自身。</p>
<h2>engines.stopAllAndToast()<span><a class="mark" href="#engines_engines_stopallandtoast" id="engines_engines_stopallandtoast">#</a></span></h2>
<p>停止所有正在运行的脚本并显示停止的脚本数量。包括当前脚本自身。</p>
<h2>engines.myEngine()<span><a class="mark" href="#engines_engines_myengine" id="engines_engines_myengine">#</a></span></h2>
<p>返回当前脚本的脚本引擎对象(<a href="#engines_scriptengine">ScriptEngine</a>)</p>
<p><strong>[v4.1.0新增]</strong>
特别的，该对象可以通过<code>execArgv</code>来获取他的运行参数，包括外部参数、intent等。例如：</p>
<pre><code>log(engines.myEngine().execArgv);
</code></pre><p>普通脚本的运行参数通常为空，通过定时任务的广播启动的则可以获取到启动的intent。</p>
<h2>engines.all()<span><a class="mark" href="#engines_engines_all" id="engines_engines_all">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a></li>
</ul>
</div><p>返回当前所有正在运行的脚本的脚本引擎<a href="#engines_scriptengine">ScriptEngine</a>的数组。</p>
<pre><code>log(engines.all());
</code></pre><h1>ScriptExecution<span><a class="mark" href="#engines_scriptexecution" id="engines_scriptexecution">#</a></span></h1>
<p>执行脚本时返回的对象，可以通过他获取执行的引擎、配置等，也可以停止这个执行。</p>
<p>要停止这个脚本的执行，使用<code>exectuion.getEngine().forceStop()</code>.</p>
<h2>ScriptExecution.getEngine()<span><a class="mark" href="#engines_scriptexecution_getengine" id="engines_scriptexecution_getengine">#</a></span></h2>
<p>返回执行该脚本的脚本引擎对象(<a href="#engines_scriptengine">ScriptEngine</a>)</p>
<h2>ScriptExecution.getConfig()<span><a class="mark" href="#engines_scriptexecution_getconfig" id="engines_scriptexecution_getconfig">#</a></span></h2>
<p>返回该脚本的运行配置(<a href="#engines_scriptconfig">ScriptConfig</a>)</p>
<h1>ScriptEngine<span><a class="mark" href="#engines_scriptengine" id="engines_scriptengine">#</a></span></h1>
<p>脚本引擎对象。</p>
<h2>ScriptEngine.forceStop()<span><a class="mark" href="#engines_scriptengine_forcestop" id="engines_scriptengine_forcestop">#</a></span></h2>
<p>停止脚本引擎的执行。</p>
<h2>ScriptEngine.cwd()<span><a class="mark" href="#engines_scriptengine_cwd" id="engines_scriptengine_cwd">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
</div><p>返回脚本执行的路径。对于一个脚本文件而言为这个脚本所在的文件夹；对于其他脚本，例如字符串脚本，则为<code>null</code>或者执行时的设置值。</p>
<h2>ScriptEngine.getSource()<span><a class="mark" href="#engines_scriptengine_getsource" id="engines_scriptengine_getsource">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="#engines_scriptsource">ScriptSource</a></li>
</ul>
</div><p>返回当前脚本引擎正在执行的脚本对象。</p>
<pre><code>log(engines.myEngine().getSource());
</code></pre><h2>ScriptEngine.emit(eventName[, ...args])<span><a class="mark" href="#engines_scriptengine_emit_eventname_args" id="engines_scriptengine_emit_eventname_args">#</a></span></h2>
<div class="signature"><ul>
<li><code>eventName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> 事件名称</li>
<li><code>...args</code> <span class="type">&lt;any&gt;</span> 事件参数</li>
</ul>
</div><p>向该脚本引擎发送一个事件，该事件可以在该脚本引擎对应的脚本的events模块监听到并在脚本主线程执行事件处理。</p>
<p>例如脚本receiver.js的内容如下：</p>
<pre><code>//监听say事件
events.on(&quot;say&quot;, function(words){
    toastLog(words);
});
//保持脚本运行
setInterval(()=&gt;{}, 1000);
</code></pre><p>同一目录另一脚本可以启动他并发送该事件：</p>
<pre><code>//运行脚本
var e = engines.execScriptFile(&quot;./receiver.js&quot;);
//等待脚本启动
sleep(2000);
//向该脚本发送事件
e.getEngine().emit(&quot;say&quot;, &quot;你好&quot;);
</code></pre><h1>ScriptConfig<span><a class="mark" href="#engines_scriptconfig" id="engines_scriptconfig">#</a></span></h1>
<p>脚本执行时的配置。</p>
<h2>delay<span><a class="mark" href="#engines_delay" id="engines_delay">#</a></span></h2>
<div class="signature"><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul>
</div><p>延迟执行的毫秒数</p>
<h2>interval<span><a class="mark" href="#engines_interval" id="engines_interval">#</a></span></h2>
<div class="signature"><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul>
</div><p>循环运行时两次运行之间的时间间隔</p>
<h2>loopTimes<span><a class="mark" href="#engines_looptimes" id="engines_looptimes">#</a></span></h2>
<div class="signature"><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
</ul>
</div><p>循环运行次数</p>
<h2>getPath()<span><a class="mark" href="#engines_getpath" id="engines_getpath">#</a></span></h2>
<div class="signature"><ul>
<li>返回 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" class="type">&lt;Array&gt;</a></li>
</ul>
</div><p>返回一个字符串数组表示脚本运行时模块寻找的路径。</p>

      </div>
    </div>
  </div>
  <script src="assets/sh_main.js"></script>
  <script src="assets/sh_javascript.min.js"></script>
  <script>highlight(undefined, undefined, 'pre');</script>
  <!-- __TRACKING__ -->
</body>
</html>